use serde_derive::Deserialize;
use toml::value::*;
 
use std::fs::File;
use std::io::prelude::*;

#[derive(Deserialize)]
#[derive(Debug)]
pub struct Config {
    pub db : Db,//数据库配置
    pub jwt : Jwt ,//jwt配置
}

//数据库配置
#[derive(Deserialize)]
#[derive(Debug)]
pub struct Db {
    pub host :String ,
    pub port :String ,
    pub username :String ,
    pub password :String ,
    pub dbname :String ,
}

#[derive(Deserialize)]
#[derive(Debug)]
pub struct Jwt {
    pub secret :String ,
}


//配置项
lazy_static! {
    pub static ref CONFIG:Config=loadConfig();
 }
//加载配置项
 pub fn loadConfig() ->Config {
    let file_path = "Config.toml";
    let mut file = match File::open(file_path) {
        Ok(f) => f,
        Err(e) => panic!("no such file {} exception:{}", file_path, e)
    };
    let mut str_val = String::new();
    match file.read_to_string(&mut str_val) {
        Ok(s) => s
        ,
        Err(e) => panic!("Error Reading file: {}", e)
    };
    let config : Config = toml::from_str(&str_val).unwrap();

    return config
 }
